
/* needed includes */
#include <fstream>

#include "GameState/Options/OptionsMenu.h"
#include "Engine/Synaptic/Misc.h"
#include "GameState/DataStorage.h"

namespace GameState {

namespace Options {

OptionEntry::OptionEntry(const Misc::Frame & frame, std::string name) : 
	MenuEntry(frame, name),
	Reason("optionsGameState"), Sense("optionsGameState"), Visual("optionsGameState"),
	focused(false)  {
}

OptionEntry::~OptionEntry() {
}

PictureTargetOption::PictureTargetOption(const Misc::Frame & frame) : 
	OptionEntry(frame, "pictureTargetOption"),
	focusGfx("menuFocus500x250", "interface", 10)  {
	std::fstream levelFile("config/pictures.txt");
	std::string levelName;
	targetList.push_back(DataStorage<std::string>::get("defaultLevel"));
	while (!levelFile.eof()) {
		std::getline(levelFile, levelName);
		if (levelName == "") continue;
		targetList.push_back(levelName);
	}
	targetList.unique();
	currentTarget = targetList.begin();
	focusGfx.setPosition(frame.x, frame.y).setFrameSize(frame.w, frame.h);
	loadedGraphic = new Engine::Resource::Graphic(*currentTarget, "board", 0);
	loadedGraphic->setFrameSize(frame.w, frame.h);
	loadedGraphic->setPosition(frame.x, frame.y);
	
}

// Menu entry was selected
void PictureTargetOption::select() {
}

void PictureTargetOption::pulse(int delta) {
}

// Asks for generation of a graphical model to be displayed
Engine::Resource::GraphicList PictureTargetOption::display() {
	Engine::Resource::GraphicList gfxList;
	gfxList.push_back(loadedGraphic);
	if (focused) gfxList.push_back(&focusGfx);
	return gfxList;
}

// Menu entry have focus.
void PictureTargetOption::haveFocus(bool focused) {
	this->focused = focused;
}

void PictureTargetOption::keyDown(SDLKey sym, SDLMod mod, Uint16 unicode) {
	if (!focused) return;
	if (sym == SDLK_RIGHT) {
		currentTarget++;
		if (currentTarget == targetList.end()) currentTarget = targetList.begin();
	} else if (sym == SDLK_LEFT) {
		if (currentTarget == targetList.begin()) currentTarget = targetList.end();
		currentTarget--;
	}
	*loadedGraphic = Engine::Resource::Graphic(*currentTarget, "board", 0);
	loadedGraphic->setFrameSize(frame.w, frame.h);
	loadedGraphic->setPosition(frame.x, frame.y);
	DataStorage<std::string>::set("selectedLevel", *currentTarget);
}

LevelOption::LevelOption(const Misc::Frame & frame) : 
	OptionEntry(frame, "levelOption"),
	focusGfx("menuFocus500x50", "interface", 10),
	levelString("level", "screen/options", 10),
	easyMode("easyMode", "screen/options", 10),
	hardMode("hardMode", "screen/options", 10)  {
	focusGfx.setPosition(frame.x, frame.y).setFrameSize(frame.w, frame.h);
	levelString.setPosition(frame.x+10, frame.y+5);
	easyMode.setPosition(frame.x+100, frame.y+5).setFrameSize(180, 35);
	hardMode.setPosition(frame.x+300, frame.y+5).setFrameSize(180, 35);
	
	bool hardModeSetted = DataStorage<bool>::get("hardModeSetted");
	easyMode.setFrame((!hardModeSetted) * 9, 0);
	hardMode.setFrame(hardModeSetted * 9, 0);

}

// Menu entry was selected
void LevelOption::select() {
}

void LevelOption::pulse(int delta) {
}

// Asks for generation of a graphical model to be displayed
Engine::Resource::GraphicList LevelOption::display() {
	Engine::Resource::GraphicList gfxList;
	if (focused) gfxList.push_back(&focusGfx);
	gfxList.push_back(&levelString);
	gfxList.push_back(&easyMode);
	gfxList.push_back(&hardMode);
	return gfxList;
}

// Menu entry have focus.
void LevelOption::haveFocus(bool focused) {
	this->focused = focused;
}

void LevelOption::keyDown(SDLKey sym, SDLMod mod, Uint16 unicode) {
	if (!focused) return;
	bool hardModeSetted = DataStorage<bool>::get("hardModeSetted");
	switch (sym) {
		case SDLK_LEFT:
		case SDLK_RIGHT:
			hardModeSetted = !hardModeSetted;
			DataStorage<bool>::set("hardModeSetted", hardModeSetted);
			easyMode.setFrame((!hardModeSetted) * 9, 0);
			hardMode.setFrame(hardModeSetted * 9, 0);
			break;
		default:
			break;
	}
}

SoundThemeOption::SoundThemeOption(const Misc::Frame & frame) :
	OptionEntry(frame, "soundThemeOption"),
	focusGfx("menuFocus500x50", "interface", 10)  {
	focusGfx.setPosition(frame.x, frame.y).setFrameSize(frame.w, frame.h);
}

// Menu entry was selected
void SoundThemeOption::select() {
}

void SoundThemeOption::pulse(int delta) {
}

// Asks for generation of a graphical model to be displayed
Engine::Resource::GraphicList SoundThemeOption::display() {
	Engine::Resource::GraphicList gfxList;
	if (focused) gfxList.push_back(&focusGfx);
	return gfxList;
}

// Menu entry have focus.
void SoundThemeOption::haveFocus(bool focused) {
	this->focused = focused;
}


} // namespace GameState::Options

} // namespace GameState
